package com.sheep.util;

import com.sheep.api.bean.ADSBBean;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 16-3-30
 * Time: 下午2:34
 * To change this template use File | Settings | File Templates.
 */
public class FileSupport {

    public static void writeTotal(String path, List<ADSBBean> list, int size) {
        int start = 0;
        while (start + size <= list.size()) {
            List<ADSBBean> temp = list.subList(start, start + size);
            start = start + size;
            writeToTxt(path, temp);
        }
    }

    public static void main(String[] args) {
        List<String> list = new ArrayList<String>();
        for (int i = 0; i < 50; i++) {
            list.add(String.valueOf(i));
        }
        int start = 0;
        int size = 10;
        while (start + size <= list.size()) {
            List<String> temp = list.subList(start, start + size);
            start = start + size;
            for (String i : temp) {
                System.out.println(i);
            }
        }
    }


    public static void writeToTxt(String path, List<ADSBBean> list) {
        FileWriter fw = null;
        BufferedWriter writer = null;
        try {
            File file = new File(path);
            if (!file.exists()) {
                file.createNewFile();
            }
            fw = new FileWriter(file,true);
            writer = new BufferedWriter(fw);
            for (ADSBBean i : list) {
                writer.append(JsonSupport.convertASDB2JSON(i));
                writer.newLine();
            }
            writer.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                writer.close();
                fw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
